/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BankClientPrototype;

import java.sql.*;
/**
 *
 * @author sjt5097
 */
public class ClientHandler {

    protected String UserAuthCode = null;

    protected ClientHandler(){

    }

    protected String getAuthCode(){
        if(UserAuthCode != null)
        return UserAuthCode;
        return "Invalid";
    }

    private void setAuthCode(){

    }

    protected void setFName(String FName){

    }

    protected void setLName(String LName){

    }

    public boolean registering(String userName, String password, int savings, int checking){
        if(savings != 0)
        {
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:Client");
                Statement stmt =con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT A.AccountNumber, A.AccountType FROM Accounts A WHERE A.AccountType='S' AND A.AccountNumber="+savings+"");

                while(rs.next()){
//                    ResultSet rs2 = stmt.executeQuery("SELECT AccountID FROM Accounts WHERE AccountNumber="+savings+"");
//                    while(rs2.next()){
//                        String temp = rs2.getString("AccountID");
                        stmt.executeUpdate("INSERT INTO OnlineInfo (AccountID, UserName, Password) VALUES ('(SELECT Accounts.AccountID FROM Accounts WHERE Accounts.AccountNumber="+savings+")', '"+userName+"', '"+password+"') WHERE Accounts.AccountNumber="+savings+"");
                        //FROM OnlineInfo INNER JOIN Accounts ON OnlineInfo.AccountID = Accounts.AccountID WHERE Accounts.AccountNumber = '"+savings+"'
                        // WHERE exists (SELECT AccountID FROM Accounts WHERE Accounts.AccountID = OnlineInfo.AccountID)
                        //WHERE Accounts.AccountID = OnlineInfo.AccountID AND Username IS NULL
//                    }
                    
                    return true;
                    }
                }
            catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return false;
        }

                if(checking != 0)
        {
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:Client");
                Statement stmt =con.createStatement();
                stmt.executeUpdate("SELECT A.AccountNumber, A.AccountType FROM Accounts A WHERE A.AccountType='C' AND A.AccountNumber='"+savings+"'");
                }
            catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return false;
        }



        return false;
    }

//////////////////////////    public void InsertMethod(String userName, String password, int account){
//////////////////////////        try{
//////////////////////////                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//////////////////////////                Connection con = DriverManager.getConnection("jdbc:odbc:Client");
//////////////////////////                Statement stmt =con.createStatement();
//////////////////////////                String temp = stmt.executeQuery("SELECT Accounts.AccountID FROM Accounts WHERE Accounts.AccountNumber="+account+")").toString();
//////////////////////////                stmt.executeUpdate("INSERT INTO OnlineInfo (UserName, Password, AccountID) VALUES('"+userName+"','"+password+"','"+temp+"')");
//////////////////////////    } catch (Exception e) {
//////////////////////////                e.printStackTrace();
//////////////////////////            }
////////////////////////
////////////////////////    }
}
